package com.ruoyi.web.controller.snmp;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SnmpPing;
import com.ruoyi.system.service.ISnmpPingService;
import com.ruoyi.web.handler.SpringWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Controller
 *
 * @author ruoyi
 * @date 2025-02-05
 */
@RestController
@RequestMapping("/snmp/ping")
public class SnmpPingController extends BaseController
{
    @Autowired
    private ISnmpPingService snmpPingService;
    private final SpringWebSocketHandler webSocketHandler;

    @Autowired
    public SnmpPingController(SpringWebSocketHandler webSocketHandler) {
        this.webSocketHandler = webSocketHandler;
    }
    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('snmp:ping:list')")
    @GetMapping("/list")
    public TableDataInfo list(SnmpPing snmpPing)
    {
        startPage();
        List<SnmpPing> list = snmpPingService.selectSnmpPingList(snmpPing);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */
    @PreAuthorize("@ss.hasPermi('snmp:ping:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SnmpPing snmpPing)
    {
        List<SnmpPing> list = snmpPingService.selectSnmpPingList(snmpPing);
        ExcelUtil<SnmpPing> util = new ExcelUtil<SnmpPing>(SnmpPing.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('snmp:ping:query')")
    @GetMapping(value = "/{ip}")
    public AjaxResult getInfo(@PathVariable("ip") String ip)
    {
        return success(snmpPingService.selectSnmpPingByIp(ip));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('snmp:ping:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SnmpPing snmpPing) throws Exception {
        return toAjax(snmpPingService.insertSnmpPing(snmpPing));
    }


    @PostMapping("/byone")
    public AjaxResult addIp(@RequestBody SnmpPing snmpPing) throws Exception {
        return toAjax(snmpPingService.insertOneSnmpPing(snmpPing));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('snmp:ping:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SnmpPing snmpPing)
    {
        return toAjax(snmpPingService.updateSnmpPing(snmpPing));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('snmp:ping:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ips}")
    public AjaxResult remove(@PathVariable String[] ips)
    {
        return toAjax(snmpPingService.deleteSnmpPingByIps(ips));
    }
}
